package com.qsd.TeacherSystem.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.qsd.common.po.*;
import com.qsd.common.pojo.PaperEntity;
import com.qsd.common.util.PageUtil;
import com.qsd.common.util.ResultMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qsd.TeacherSystem.Service.CourseService;
import com.qsd.TeacherSystem.Service.PaperService;
import com.qsd.TeacherSystem.dao.ClazzDao;
import com.qsd.TeacherSystem.dao.CourseDao;
import com.qsd.common.util.JdbcUtil;
import com.qsd.common.vo.PageVo;

/**
 * @Description 视图层跳转
 * @Author Esion
 * @Data 2020年6月20日
 */
@Slf4j
@Controller
public class IndexController {

    @Resource
    private CourseDao courseDao;
    @Resource
    private ClazzDao clazzDao;
    @Resource
    private PaperService paperService;
    @Resource
    private CourseService courseService;

    @RequestMapping("s")
    public String webSocket  () {
        log.info("测试webSocket");
        return "webSocket";
    }


    @RequestMapping("index")
    public String index() {
        log.info("跳转到首页");
        return "index";
    }

    @RequestMapping("self")
    public ModelAndView self(HttpServletRequest request) {
        log.info("获取当前登录用户信息");
        ModelAndView modelAndView = new ModelAndView("self");
        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        log.info("当前登录用户信息:{}", teacher);
        modelAndView.addObject("teacher", teacher);
        return modelAndView;
    }

    /**
     * 全部课程
     */
    @RequestMapping("course")
    public String course(int page, int limit, HttpServletRequest request, Model model) {
        log.info("跳转到课程管理页");
        if (page < 1) page = 1;
        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        List<Course> courses = courseDao.queryCourseByTeacher(teacher.getUsername());
        PageVo<Course> result = JdbcUtil.PageUtil(page, limit, courses);
        model.addAttribute("courses", result.getData());
        model.addAttribute("count", result.getCount());
        model.addAttribute("page", page);
        return "course";
    }

    @GetMapping("paper")
    public String paper(int page, int limit, Model model, HttpServletRequest request) {
        log.info("跳转到试卷管理页");
        if (page < 1) page = 1;
        //count, page, papers, courses
        getPaperList(page, limit, model, request);
        return "paper";
    }

    private void getPaperList(int page, int limit, Model model, HttpServletRequest request) {
        model.addAttribute("page", page);
        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        String username = teacher.getUsername();
        PageVo<Paper> papers = paperService.papers(page, limit, username);
        List<Course> courses = courseDao.queryCourseByTeacher(username);
        model.addAttribute("count", papers.getCount());
        model.addAttribute("papers", papers.getData());
        model.addAttribute("courses", courses);
        log.info("count:{}", papers.getCount());
    }


    @GetMapping("paper2")
    public String paper2(int page, int limit, Model model, HttpServletRequest request) {
        log.info("跳转到题库管理页");
        if (page < 1) page = 1;
        //count, page, papers, courses
        getPaperList2(page, limit, model, request);
        return "paper_tk";
    }

    private void getPaperList2(int page, int limit, Model model, HttpServletRequest request) {
        model.addAttribute("page", page);
        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        String username = teacher.getUsername();
        PageVo<Paper> papers = paperService.papers2(page, limit, username);
        List<Course> courses = courseDao.queryCourseByTeacher(username);
        model.addAttribute("count", papers.getCount());
        model.addAttribute("papers", papers.getData());
        model.addAttribute("courses", courses);
        log.info("count:{}", papers.getCount());
    }


    /**
     * 删除整套试卷
     *
     * @param id
     * @return
     */
    @GetMapping("deletePage")
    public String deletePage(Integer id, Model model, HttpServletRequest request) {
        log.info("删除试卷:type{},id", id);
        paperService.deletePage(id);
        //count, page, papers, courses
        int page = 1;
        getPaperList(page, 9, model, request);
        return "paper";
    }
    /**
     * 删除整套试卷
     *
     * @param id
     * @return
     */
    @GetMapping("deletePage2")
    public String deletePage2(Integer id, Model model, HttpServletRequest request) {
        log.info("删除试卷:type{},id", id);
        paperService.deletePage(id);
        //count, page, papers, courses
        int page = 1;
        getPaperList2(page, 9, model, request);
        return "paper_tk";
    }

    @RequestMapping("grade")
    public String grade() {
        log.info("跳转到成绩信息页");
        return "grade";
    }

    @RequestMapping("about")
    public String about() {
        log.info("跳转到系统介绍页面");
        return "about";
    }

    @RequestMapping("addcourse")
    public String addcourse() {
        return "addcourse";
    }

    @RequestMapping("info/{id}")
    public String info(@PathVariable("id") Integer id, Model model, HttpServletRequest request) {
        //课程信息
        Course course = courseDao.queryCourseById(id);
        //检查课程所属
        String courseTeacher = course.getTeacherUsername();
        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        if (teacher.getUsername().equals(courseTeacher)) {
            //班级信息
            List<Student> students = clazzDao.queryAllStudents(course.getId());
            model.addAttribute("students", students);
        }
        model.addAttribute("course", course);
        return "info";
    }

    @GetMapping("paper/info")
    public String paperInfo(Integer id, Model model) {
        Paper paper = paperService.queryPaperById(id);
        model.addAttribute("paper", paper);
        return "paper/info";
    }

    @GetMapping("paper/edit")
    public String paperEdit(Integer id, Model model) {
        Paper paper = paperService.queryPaperById(id);
        model.addAttribute("paper", paper);
        return "paper/edit";
    }

    @GetMapping("answerList")
    public String answerList(int page, int limit, Model model, HttpServletRequest request) {
        log.info("跳转到试卷管理页面");
        //获取TeacherUsername
        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        PageVo<Answer> answerList = courseService.answerList(page, limit, teacher.getUsername());
        model.addAttribute("page", page);
        model.addAttribute("answers", answerList.getData());
        model.addAttribute("count", answerList.getCount());
        return "answerList";
    }


    @RequestMapping("asl2")
    public String asl2Paper2() {
        log.info("");
        return "answerList2";
    }

    /**
     * 查询考试分数列表,条件搜索
     *
     * @param grade
     * @param page
     * @return
     */
    @RequestMapping(value = "/answerList2")
    @ResponseBody
    public ResultMsg paperList2(Paper grade, PageUtil page) {
        log.info("试卷管理2");
        List<PaperEntity> iPage = paperService.paperList2(grade, page);
        return new ResultMsg(0, "", iPage, page.getCount());
    }


    /**
     * 生成试卷
     *
     * @param paperId
     * @param model
     * @return
     */
    @GetMapping("paper/answer")
    public String paperAnswer(Integer paperId, Model model) {
        log.info("调用IndexController下,生成试卷");
        Paper paper = paperService.queryPaperById(paperId);
        model.addAttribute("paper", paper);
      /*  Answer answer = courseService.check(id);
        model.addAttribute("answer", answer);*/
        return "paper/answer";
    }

    /**
     * 组卷
     *
     * @param paperId
     * @param model
     * @return
     */
    @GetMapping("paper/answer2")
    public String paperAnswer2(Integer paperId, Model model) {
        log.info("调用IndexController下,自动组成试卷");
        Paper paper = paperService.queryPaperById2(paperId);
        model.addAttribute("paper", paper);
        return "paper/answer";
    }


    /**
     * 批改试卷
     *
     * @param id
     * @param paperId
     * @param model
     * @return
     */
    @Deprecated
    @GetMapping("paper/check")
    public String paperCheck(Integer id, Integer paperId, Model model) {
        Paper paper = paperService.queryPaperById(paperId);
        model.addAttribute("paper", paper);
        Answer answer = courseService.check(id);
        model.addAttribute("answer", answer);
        return "paper/check";
    }

}
